home *** CD-ROM | disk | FTP | other *** search
/ Acorn User: China / Acorn User China CD-ROM (UK) (Disc A) / Acorn User China CD-ROM (UK) (Disc A).bin / DEMON / MISC / NETLITE2.ARC / NET / c / ASY < prev    next >
Encoding:
Text File  |  1993-04-24  |  4.9 KB  |  212 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "werr.h"
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "internet.h"
  9. #include "iface.h"
  10. #include "cmdparse.h"
  11. #include "misc.h"
  12. #include "asy.h"
  13. #include "slip.h"
  14. #include "arc.h"
  15. #include "os.h"
  16. #include "swis.h"
  17.  
  18. struct asy asy[ASY_MAX];
  19. unsigned int nasy;
  20. extern struct interface *ifaces;
  21.  
  22. extern char nospace[];
  23.  
  24. static int asy_init(int16, struct interface *, int);
  25. static int asy_stop(struct interface *);
  26. static int asy_speed(int16, int);
  27. static int asy_output(int16, char *, int);
  28. static int asy_recv(int16, char *, int);
  29.  
  30. /* Attach a serial interface to the system
  31.  * argv[0]: hardware type, must be "asy"
  32.  * argv[1]: async driver name, must be "internal"
  33.  * argv[2]: port number, must "0"
  34.  * argv[3]: mode, may be:
  35.  *          "slip" (point-to-point SLIP)
  36.  * argv[4]: interface label, e.g., "sl0"
  37.  * argv[5]: maximum transmission unit, bytes
  38.  * argv[6]: interface speed, e.g, "9600"
  39.  */
  40. int asy_attach(int argc, char **argv)
  41. {
  42.         struct interface *if_asy;
  43.         struct slip *if_slip;
  44.         int16 dev;
  45.         int   mode;
  46.  
  47.         argc = argc;
  48.  
  49.         if (nasy >= ASY_MAX) {
  50.                 werr(1, "Too many async controllers");
  51.                 return -1;
  52.         }
  53.  
  54.         if (strcmp(argv[1], "internal") != 0) {
  55.                 werr(1, "Unknown serial port %s", argv[1]);
  56.                 return -1;
  57.         }
  58.  
  59.         if (strcmp(argv[2], "0") != 0) {
  60.                 werr(1, "Unknown serial port %s", argv[2]);
  61.                 return -1;
  62.         }
  63.  
  64.         if (strcmp(argv[3],"slip") == 0)
  65.                 mode = SLIP_MODE;
  66.         else
  67.                 mode = UNKNOWN;
  68.  
  69.         dev = nasy++;
  70.  
  71.         /* Create interface structure and fill in details */
  72.         if_asy  = (struct interface *)calloc(1,sizeof(struct interface));
  73.  
  74.         if_asy->name = strdup(argv[4]);
  75.         if_asy->mtu  = atoi(argv[5]);
  76.         if_asy->dev  = dev;
  77.         if_asy->recv = doslip;
  78.         if_asy->stop = asy_stop;
  79.         if_asy->put  = asy_output;
  80.         if_asy->get  = asy_recv;
  81.  
  82.         switch(mode){
  83.         case SLIP_MODE:
  84.                 if_slip = (struct slip *)calloc(1,sizeof(struct slip));
  85.                 if_asy->slip   = if_slip;
  86.                 if_asy->send   = slip_send;
  87.                 if_asy->raw    = slip_raw;
  88.                 if_asy->slip->recv = slip_recv;
  89.                 break;
  90.         default:
  91.                 werr(1, "Mode %s unknown for interface %s", argv[3], argv[4]);
  92.                 free(if_asy->name);
  93.                 free(if_asy);
  94.                 nasy--;
  95.                 return -1;
  96.         }
  97.  
  98.         if_asy->next = ifaces;
  99.         ifaces = if_asy;
  100.         asy_init(dev, if_asy, atoi(argv[2]));
  101.         asy_speed(dev, atoi(argv[6]));
  102.         return 0;
  103. }
  104.  
  105. /* Initialize asynch port "dev" */
  106. static int asy_init(int16 dev, struct interface *iface, int port)
  107. {
  108.         os_regset regs;
  109.  
  110.         asy[dev].iface  = iface;
  111.         asy[dev].port   = port;
  112.         asy[dev].speed  = 0;
  113.  
  114.         /* Start up serial interrupts */
  115.         regs.r[0] = 2;
  116.         regs.r[1] = 2;
  117.         os_swix(OS_Byte, ®s);
  118.  
  119.         regs.r[0] = 1;
  120.         regs.r[1] = 0x00;
  121.         os_swix(OS_SerialOp, ®s);
  122.  
  123.         return(1);
  124. }
  125.  
  126. static int asy_stop(struct interface *interface)
  127. {
  128.         os_regset regs;
  129.  
  130.         interface = interface;
  131.  
  132.         /* Stop serial interrupts */
  133.         regs.r[0] = 2;
  134.         regs.r[1] = 0;
  135.         os_swix(OS_Byte, ®s);
  136.  
  137.         return(0);
  138. }
  139.  
  140. static struct
  141. {
  142.        int real_speed;
  143.        int arc_speed;
  144. }
  145. speed_table[] =
  146. {
  147.        {300,   3},
  148.        {1200,  4},
  149.        {2400,  5},
  150.        {4800,  6},
  151.        {9600,  7},
  152.        {19200, 8}
  153. };
  154.  
  155. #define ARC_SPEED_COUNT   6
  156.  
  157. /* Set asynch line speed */
  158. static int asy_speed(int16 dev, int speed)
  159. {
  160.         os_regset regs;
  161.         int i;
  162.  
  163.         if (speed == 0 || dev >= nasy) return(-1);
  164.  
  165.         for (i = 0; i < ARC_SPEED_COUNT; i++)
  166.         {
  167.                  if (speed_table[i].real_speed == speed)
  168.                  {
  169.                         asy[dev].speed = speed;
  170.  
  171.                         regs.r[0] = 5;
  172.                         regs.r[1] = speed_table[i].arc_speed;
  173.                         os_swix(OS_SerialOp, ®s);
  174.  
  175.                         regs.r[0] = 6;
  176.                         regs.r[1] = speed_table[i].arc_speed;
  177.                         os_swix(OS_SerialOp, ®s);
  178.  
  179.                         return(0);
  180.                  }
  181.         }
  182.  
  183.  
  184.         werr(0, "Invalid speed %d", speed);
  185.  
  186.         return(-1);
  187. }
  188.  
  189. /* Send a buffer to serial transmitter */
  190. static int asy_output(int16 dev, char *buf, int cnt)
  191. {
  192.         register int i;
  193.  
  194.         if (dev >= nasy) return(0);
  195.  
  196.         for (i = 0; i < cnt && ser_putc(*buf) != 0; i++, buf++);
  197.  
  198.         return(i);
  199. }
  200.  
  201. /* Receive characters from asynch line
  202.  * Returns count of characters read
  203.  */
  204. static int asy_recv(int16 dev, char *buf, int cnt)
  205. {
  206.         if (dev >= nasy || cnt < 1) return(0);
  207.  
  208.         return((ser_getc(buf) != 0) ? 1 : 0);
  209. }
  210.  
  211.  
  212.